#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Git Fetch Reset Assets ===
function gitfetchtreset()
{
  # Usage: gitfetchtreset [remote] [branch] (Git fetch and reset hard)

  # Fetch and reset hard
  git fetch "${1:-$(githubusername)}" "${2:-$(repogetbranch)}";
  git reset FETCH_HEAD --hard;
}

# === GitHub Url Fetch-Reset ===
function gitfcu()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: gitfcu <url> [branch] (Git fetch url and reset)';
    echo '';
    return;
  fi;

  # Variables
  link=${1};

  if [ ! -z "${2}" ]; then
    branch=${2};
  else
    branch=cm-14.1;
  fi;

  if [ -z "${link}" ]; then
    echo " Error: Not a GitHub url '${link}'";
    return;
  fi;

  git fetch "${link}" "${branch}";
  echo '';
  git reset --hard FETCH_HEAD;
}

# === Git Full Url Fetch ===
function gitfurl()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: gitfurl <url> (Git fetch from full url)';
    echo '';
    return;
  fi;

  # Variables
  local url=${1};
  local branch;

  # Prepare url
  branch=${url};
  url=${url%/commits/*};
  url=${url%/tree/*};
  url=${url%/+/*};
  branch=${branch#*/commits/};
  branch=${branch#*/tree/};
  branch=${branch#*/+/};

  # Git fetch from full url
  git fetch "${url}" "${branch}";
}

# === Git Unshallow Project ===
function gitunshallow()
{
  # Usage: gitunshallow (Git fetch and unshallow project)

  # Variables
  local currentdir=${PWD};
  local branch;
  local submodules;

  # Fetch and unshallow
  branch=$(repogetbranch);
  git fetch --unshallow "$(gitgetremote "${branch}")" "${branch}" 2>&1 \
    | grep -v 'on a complete repository does not make sense';

  # Fetch and unshallow submodules
  submodules=$(git submodule--helper list | cut -f 2);
  for path in ${submodules}; do
    cd "${currentdir}/${path}/";
    branch=$(repogetbranch);
    git fetch --unshallow "$(gitgetremote "${branch}")" "${branch}" 2>&1 \
      | grep -v 'on a complete repository does not make sense';
  done;

  # Restore current dir
  cd "${currentdir}/";
}

# === Git Fetch Shortcuts ===
alias gitf='git fetch';
alias gitfmr='git fetch origin; git reset origin/master';
alias gitfs='git fetch origin; git reset origin/$(git rev-parse --abbrev-ref HEAD); git stash';
alias gitfsu='git fetch origin; git reset origin/$(git rev-parse --abbrev-ref HEAD); git stash -p';
alias gitfgr='gitfetchtreset github';
alias gitfor='gitremoteverify origin gitrao; gitfetchtreset origin';
alias gitforla='gitfetchtreset origin aosp/LA.UM.5.5_rb1.10';
alias gitfar='gitremoteverify $(githubusername) gitraa; gitfetchtreset $(githubusername)';
alias gitfprivr='gitfetchtreset private';
